// pages/teacher/teacherDetail/teacherDetail.js
const common = require('../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        teacherId: null,
        teacherInfo: {},
        teacherImg: [],
        teacherDetail: '',
        courseList: [],
        navType: 1,
        nowPage: 0,
        isLoading: false,
        isAll: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            teacherId: options.teacherId
        })
        this.getTeacherInfo()
        // this.getCourseList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        this.getCourseList()
    },

    getTeacherInfo: function() {
        const that = this
        wx.showLoading({
            title: '加载中',
        })
        let data = {
            activity: 785,
            teacher_id: this.data.teacherId
        }
        common.post('Online/get_online_teacher', data).then(function(res) {
            wx.hideLoading()
            if (res.code == 204) {
                that.setData({
                    teacherInfo: {},
                })
            } else {
                let teacherImg = []
                let teacherDetail = ''
                if (res[0].image_vice) {
                    teacherImg = res[0].image_vice.split(',')
                }
                if (res[0].image_info) {
                    teacherDetail = res[0].image_info.replace(/\<img/gi, '<img style="display:block;width:100%;height:auto" ')
                }
                teacherImg.unshift(res[0].image)
                that.setData({
                    teacherInfo: res[0],
                    teacherImg: teacherImg,
                    teacherDetail: teacherDetail
                })
            }
        }).catch(function(e) {})
    },

    getCourseList: function() {
        if (this.data.isLoading) {
            return
        }
        if (this.data.isAll) {
            return
        }
        const that = this
        let courseList = this.data.courseList
        let data = {
            activity: 785,
            teacher_id: this.data.teacherId,
            page: this.data.nowPage,
            limit: 10
        }
        if (this.data.navType == 2) {
            this.setData({
                isLoading: true
            })
            wx.showLoading({
                title: '加载中',
            })
            common.post('Online/online_list', data).then(function(res) {
                that.setData({
                    isLoading: false
                })
                wx.hideLoading()
                if (res.code == 204) {
                    that.setData({
                        courseList: courseList,
                        isAll: true
                    })
                } else {
                    courseList = courseList.concat(res)
                    that.setData({
                        courseList: courseList,
                        nowPage: that.data.nowPage + 1
                    })
                }
            }).catch(function(e) {})
        } else if (this.data.navType == 3) {
            this.setData({
                isLoading: true
            })
            wx.showLoading({
                title: '加载中',
            })
            common.post('Online/down_online_list', data).then(function(res) {
                that.setData({
                    isLoading: false
                })
                wx.hideLoading()
                if (res.code == 204) {
                    that.setData({
                        courseList: courseList,
                        isAll: true
                    })
                } else {
                    courseList = courseList.concat(res)
                    that.setData({
                        courseList: courseList,
                        nowPage: that.data.nowPage + 1
                    })
                }
            }).catch(function(e) {})
        }
    },

    selectNav: function(e) {
        if (this.data.navType == e.currentTarget.dataset.type) {
            return
        }
        if (this.data.isLoading) {
            return
        }
        if (e.currentTarget.dataset.type == 1) {
            this.setData({
                navType: e.currentTarget.dataset.type
            })
        } else {
            this.setData({
                courseList: [],
                navType: e.currentTarget.dataset.type,
                nowPage: 0,
                isLoading: false,
                isAll: false
            })
            this.getCourseList()
        }
    },

    goCourseDetail: function(e) {
        if (e.currentTarget.dataset.type == 1) {
            wx.navigateTo({
                url: '/pages/course/voiceCourse/detail/detail?courseId=' + e.currentTarget.dataset.id
            })
        } else {
            wx.navigateTo({
                url: '/pages/course/offlineCourse/detail/detail?courseId=' + e.currentTarget.dataset.id
            })
        }
    }
})